﻿NewEntry_ModuleView = Backbone.Marionette.Layout.extend({
    template: "#NewEntry_Layout-template",
    events:{
        "click #NewEntry_Categories-save"	:	"save"
    },

    regions: {
        header: '#NewEntry_Layout-header',
        daily: '#NewEntry_Layout-daily',
        categories: '#NewEntry_Layout-categories'
    },
    initialize: function(){

    },

    save: function(event){

        var daily = MyApp.NewEntryModule.daily;

        console.log($(this.el).find('#NewEntry_Layout-sleep-m').val());

        var hours = $(this.el).find('#NewEntry_Layout-wakeup-h').val();
        var minutes = $(this.el).find('#NewEntry_Layout-wakeup-m').val();
        var waketime = hours + ":" + minutes;

        hours = $(this.el).find('#NewEntry_Layout-sleep-h').val();
        minutes = $(this.el).find('#NewEntry_Layout-sleep-m').val();
        var sleeptime = hours + ":" + minutes;

        if(sleeptime.length != 5 || waketime.length != 5){
            alert("Bitte korrekte Uhrzeiten eingeben");
        }else{
            daily.set('sleeptime', sleeptime);
            daily.set('waketime', waketime);
            MyApp.module('NewEntryModule').trigger('CategoriesView:save');
        }

    },

    onDomRefresh: function(){

        //Uhrzeit anzeigen
        $(this.el).find('#date').html(displayDate());

        //Switch Button
        $(this.el).find('#switch').bootstrapSwitch();
        $(this.el).find('#switch').on('switchChange.bootstrapSwitch', function(event, state) {
            var daily = MyApp.NewEntryModule.daily;
            if(state.value === true){
                daily.fetch({
                    data: {
                        when: "yesterday"
                    }
                });
            }else{
                daily.fetch({
                    data: {
                        when: "today"
                    }
                });
            }
        });

        //Slider Motivation
        var daily = MyApp.NewEntryModule.daily;
        var self = this;
        this.$el.find('#NewEntry_Layout-motivation').slider({
            max: 10,
            min: 0,
            step: 1,
            value: daily.get('motivation'),
            slide: function (event, ui) {
                daily.set('motivation', ui.value);
                $(self.el).find('#NewEntry_Layout-motivationValue').html(ui.value);
            }
        });

        //Slider Satisfaction
        this.$el.find('#NewEntry_Layout-satisfaction').slider({
            max: 10,
            min: 0,
            step: 1,
            value: daily.get('satisfaction'),
            slide: function (event, ui) {
                daily.set('satisfaction', ui.value);
                $(self.el).find('#NewEntry_Layout-satisfactionValue').html(ui.value);
            }
        });
    }

});


NewEntry_CategoryView = Backbone.Marionette.Layout.extend({
    template: "#NewEntry_Category-template",
    tagName: "p",
    className: "",

   
    onDomRefresh: function(){
        var self = this;
        this.$el.find('#sliderContainer').slider({
            max: self.model.get('maximum'),
            min: self.model.get('minimum'),
            step: self.model.get('step'),
            value: self.model.get('value'),
            slide: function (event, ui) {
                $(self.el).find('#amount').html(ui.value);
                self.model.set('value', ui.value);
            }
        });
    }
});

NewEntry_CategoriesView = Backbone.Marionette.CompositeView.extend({
    template: "#NewEntry_Categories-template",
    tagName: "div",
    itemView: NewEntry_CategoryView,
    itemViewContainer: '#categoryContainer',

    appendHtml: function (collectionView, itemView) {
        collectionView.$("#categoryContainer").append(itemView.el);
    },

    onShow: function(){

    }

});